<template>
	<view class="page">
		<view class="main">
			<view class="form-wrap">
				<u-form labelWidth="220rpx">
					<u-form-item label="姓名：" prop="name" required>
						<u-input v-model="member.name" border="none" inputAlign="right" placeholder="请输入姓名"
							type="nickname" />
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-form-item label="企业名称：" prop="name" required>
						<u-input v-model="company.name" border="none" inputAlign="right" placeholder="请输入企业名称" />
					</u-form-item>
					<u-form-item label="职务：" prop="position">
						<u-input v-model="member.position" border="none" inputAlign="right" placeholder="请输入职务" />
					</u-form-item>
					<u-form-item label="主营品牌及产品：" prop="zypphcp" labelPosition="top" labelWidth="100%">
						<u-textarea v-model="company.zypphcp" placeholder="请输入主营品牌及产品" count maxlength="200"
							style="margin-top: 38rpx;" height="186rpx"></u-textarea>
					</u-form-item>
				</u-form>
			</view>
			<view class="form-wrap" style="padding-bottom: 24rpx;">
				<view class="page-title">
					信息内容
				</view>
				<u-textarea v-model="form.content" placeholder="请输入信息内容" count maxlength="200"
					style="margin-top: 38rpx;" height="186rpx"></u-textarea>
			</view>
			<view class="page-title">
				产品图片
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" multiple width="144rpx" name="1"
				height="144rpx">
				<view class="add-img">
					<image src="../../../static/img/add-img.png" style="width: 144rpx;height: 144rpx;"></image>
				</view>
			</u-upload>
		</view>
		<view class="btm-wrap-tc">
		</view>
		<view class="btm-wrap">
			<view class="btn confim-btn" @click="submit">
				立即发布
			</view>
		</view>
	</view>
</template>

<script>
	import piaoyiEditor from '@/uni_modules/piaoyi-editor/components/piaoyi-editor/piaoyi-editor.vue';

	export default {
		data() {
			return {
				form: {
					content: ''
				},
				fileList1: [],
				fileList2: [],
				readOnly: false, //是否只读
				photoUrl: 'http://test.com', //服务器图片域名或者ip
				api: '/upload', //上传图片接口地址
				txt: '',
				name: 'file',
				values: '',
				member: {},
				company: {},
				firstInp: false
			};
		},
		components: {
			piaoyiEditor
		},
		onLoad() {
			this.getMemberInfo()
		},
		methods: {
			submit() {
				if (uni.$u.test.isEmpty(this.company.name)) {
					uni.showToast({
						title: "请输入企业名称",
						icon: "none"
					})
					return false
				}
				if (uni.$u.test.isEmpty(this.member.name)) {
					uni.showToast({
						title: "请输入姓名",
						icon: "none"
					})
					return false
				}
				if (uni.$u.test.isEmpty(this.form.content)) {
					uni.showToast({
						title: "请输入信息内容",
						icon: "none"
					})
					return false
				}
				let productImg = ''
				if (this.fileList1.length > 0) {
					productImg = this.fileList1.map(i => {
						return i.url
					}).join(',')
				}
				this.$api.duijie.saveGxfb({
					memberName: this.member.name,
					companyName: this.company.name,
					zypphcp: this.company.zypphcp,
					position: this.member.position,
					content: this.form.content,
					productImg: productImg
				}).then(res => {
					uni.showToast({
						title: "发布成功",
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)

				})
			},
			getMemberInfo() {
				this.$api.member.getMemberInfo().then(res => {
					this.member = res.data.member
					if (this.member.companyId) {
						this.company = res.data.company
						this.firstInp = true
					} else {
						this.firstInp = false
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$serverUrl + '/api/member/uploadFile.client', //仅为示例，非真实的接口地址
						filePath: url,
						name: 'upload',
						success: (uploadFileRes) => {
							resolve(JSON.parse(uploadFileRes.data).data)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #F5F5F5;

		.page-tips {
			width: 100%;
			background: #FCF5EF;
			color: #CC6C6C;
			height: 72rpx;
			line-height: 72rpx;
			text-align: center;
			font-weight: bold;
		}

		.main {
			position: relative;
			padding: 24rpx 30rpx;

			.page-title {
				height: 80rpx;
				line-height: 80rpx;
				font-size: 28rpx;
			}

			.form-wrap {

				background: #FFFFFF;
				border-radius: 20rpx;
				padding: 0 30rpx;
				margin-top: 24rpx;

				.tx-wrap {
					display: flex;
					width: 100%;
					justify-content: flex-end;
				}

				.tx {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;

				}
			}
		}
	}

	.btm-wrap-tc {
		width: 100%;
		height: 170rpx;

	}

	.btm-wrap {
		width: 100%;
		position: fixed;
		bottom: 50rpx;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: space-around;
		z-index: 99;

		.btn {
			width: 684rpx;
			height: 70rpx;
			border-radius: 42rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;

			&.return-btn {
				border: 1px solid #EC2424;
				color: #EC2424;
				background-color: #FFFFFF;
			}

			&.confim-btn {
				color: #FFFFFF;
				background: #EC2424;
			}
		}
	}
</style>